<?php

namespace App\Controllers;

use App\Models\Places;
use Config\Services;

class Travel extends BaseController
{
    public function index(): string
    {
        // connect to the model
        $places = new \App\Models\Places();

        // retrieve all the records
        $records = $places->findAll();

        // 创建表格实例
        $table = new \CodeIgniter\View\Table();

        // 设置表格标题（只显示 Name 和 Description）
        // 使用模型中的 fields 属性，跳过 id 字段，取 name 和 description
        $headings = $places->fields;
        $displayHeadings = array_slice($headings, 1, 2); // 跳过 id，取 name 和 description
        $table->setHeading(array_map('ucfirst', $displayHeadings));

        // 添加带链接的行
        foreach ($records as $record) {
            $nameLink = anchor("travel/showme/$record->id", $record->name);
            $table->addRow($nameLink, $record->description);
        }

        // 设置表格样式
        $template = [
            'table_open' => '<table cellpadding="5px" style="border-collapse: collapse; width: 100%;">',
            'cell_start' => '<td style="border: 1px solid #dddddd; padding: 8px;">',
            'cell_alt_start' => '<td style="border: 1px solid #dddddd; padding: 8px;">',
            'row_start' => '<tr>',
            'row_alt_start' => '<tr style="background-color: #f9f9f9;">',
        ];
        $table->setTemplate($template);

        // 模板数据
        $fields = [
            'title' => 'Travel Destinations',
            'heading' => 'Travel Destinations',
            'footer' => 'Copyright © 2024'
        ];

        // 组合模板和表格
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
            ->render('templates/top') .
            $table->generate() .
            $parser->setData($fields)
            ->render('templates/bottom');
    }

    public function showme($id): string
    {
        $places = new \App\Models\Places();
        $record = $places->find($id);
        
        // 检查记录是否存在
        if (!$record) {
            throw \CodeIgniter\Exceptions\PageNotFoundException::forPageNotFound();
        }

        // 创建表格实例
        $table = new \CodeIgniter\View\Table();
        
        // 添加详细信息行 - 使用英文标签
        $table->addRow(['ID', $record->id]);
        $table->addRow(['Name', $record->name]);
        $table->addRow(['Description', $record->description]);
        
        // 创建可点击的链接
        $link = $record->link ? anchor($record->link, 'Visit Website', ['target' => '_blank']) : 'No link';
        $table->addRow(['Link', $link]);
        
        // 显示图片
        $image = $record->image ? 
            '<img src="/images/' . $record->image . '" alt="' . $record->name . '" style="max-width: 300px; height: auto;">' : 
            'No image';
        $table->addRow(['Image', $image]);

        // 设置表格样式
        $template = [
            'table_open' => '<table cellpadding="5px" style="border-collapse: collapse; width: 100%;">',
            'cell_start' => '<td style="border: 1px solid #dddddd; padding: 8px; vertical-align: top;">',
            'cell_alt_start' => '<td style="border: 1px solid #dddddd; padding: 8px; vertical-align: top;">',
            'row_start' => '<tr>',
            'row_alt_start' => '<tr style="background-color: #f9f9f9;">',
            'heading_cell_start' => '<th style="border: 1px solid #dddddd; padding: 8px; background-color: #f2f2f2; text-align: left; width: 20%;">',
        ];
        $table->setTemplate($template);

        // 模板数据 - 使用英文
        $fields = [
            'title' => $record->name . ' - Details',
            'heading' => $record->name,
            'footer' => 'Copyright © 2024'
        ];

        // 组合模板
        $parser = \Config\Services::parser();
        return $parser->setData($fields)
            ->render('templates/top') .
            $table->generate() .
            $parser->setData($fields)
            ->render('templates/bottom');
    }
}